<template>
  <el-dialog
  title="收藏"
  :visible.sync="dialogVisible"
  width="30%"
  :before-close="handleClose">
  <div>
    <div style="height:50px;">
        <el-button type="primary" size="mini" :disabled="!collectStatus" @click="handleCollect">收藏</el-button>
        <el-input size="mini" style="width:180px;margin-left:10px" v-model="collect"></el-input>
    </div>
    <div style="height:50px;">
        <el-button type="primary" size="mini" :disabled="!lockStatus" @click="handleLock">锁定</el-button>
        <el-input size="mini" style="width:180px;margin-left:10px" v-model="lock"></el-input>
    </div>
    <div style="height:50px;">
        <el-button type="primary" size="mini" :disabled="!releaseStatus" @click="handleRelease">释放</el-button>
        <el-input size="mini" style="width:180px;margin-left:10px" v-model="release"></el-input>
    </div>
</div>
</el-dialog>
</template>
<script>
import { antennaCollect} from '../../../api/tansfer';
export default {
    props:{
        anteId:String,
        sifuDeviceNo:String,
    },
  data() {
    return {
      dialogVisible: false,
      collect:null,
      lock:null,
      release:null,
      collectStatus:0,
      lockStatus:0,
      releaseStatus:0,
    };
  },
  mounted() {
  },
  watch:{
    dialogVisible(val){
        if(val){
            this.initWebSocket();
        }else{
            this.webSocket.close();
        }
    }
  },
  methods: {
    handleCollect(){
        const param = {
            deviceNo: this.sifuDeviceNo,
            collectCode:0,
        }
        antennaCollect(param).then((res) => {
            this.$message({
                type: 'success',
                message: '设置成功'
            });
        }).catch(() => {
            this.$message({
                type: 'error',
                message: '计算失败'
            });
        });
    },
    handleLock(){
        const param = {
            deviceNo: this.sifuDeviceNo,
            collectCode:1,
        }
        antennaCollect(param).then((res) => {
            this.$message({
                type: 'success',
                message: '设置成功'
            });
        }).catch(() => {
            this.$message({
                type: 'error',
                message: '计算失败'
            });
        });
    },
    handleRelease(){
        const param = {
            deviceNo: this.sifuDeviceNo,
            collectCode:2,
        }
        antennaCollect(param).then((res) => {
            this.$message({
                type: 'success',
                message: '设置成功'
            });
        }).catch(() => {
            this.$message({
                type: 'error',
                message: '计算失败'
            });
        });
    },
    handleClose(){
        this.dialogVisible = false;
    },
    initWebSocket() {
      const wsuri = process.env.VUE_APP_WS_API + "/antenna/toec/yk/ws/1_12345678";
      this.webSocket = new WebSocket(wsuri);
      this.webSocket.onmessage = this.webSocketOnMessage;
      this.webSocket.onopen = this.webSocketOnOpen;
      this.webSocket.onerror = this.webSocketOnError;
      this.webSocket.onclose = this.webSocketClose;
    },
    webSocketOnOpen() {
      console.log("websocket已打开");
    },
    webSocketOnError() {
      console.log("websocket发生了错误");
    },
    webSocketOnMessage(e) {
      if (e.data) {
        let param = JSON.parse(e.data);
        param.map(item=>{
            if(item.anteId == this.anteId){
                this.collect = item.sifuStatusDTO.collectDown;
                this.lock = item.sifuStatusDTO.lockUnlocked
                this.release = item.sifuStatusDTO.lockReleased
                this.collectStatus = item.sifuStatusDTO.collectStatus
                this.lockStatus = item.sifuStatusDTO.lockStatus
                this.releaseStatus = item.sifuStatusDTO.releaseStatus
            }
        })
      }
    },
    webSocketClose(e) {
      console.log("断开连接", e);
    },
  },
};
</script>

<style lang="less" scoped>

</style>
